import { tongBuCWListApi, userListApi } from '@/api';
import { isLocal, isSuccess } from '@/utils';
import { Button, Card, message, Space } from 'antd';
import { useEffect, useState, useRef } from 'react';
import { ProTable, ProColumns } from '@ant-design/pro-components';
import { getYuanGong } from '@/utils/helper';
import { yuanGongLie } from '../Home/ComonOrder';
import dayjs from 'dayjs';

export default function TongBuList() {
  const tableRef: any = useRef('');
  const [yuanGongs, setYuanGongs] = useState<any>([]);
  const [defYuanGongId, setDefYuanGongId] = useState<any>('');
  const defSearchTime = dayjs().subtract(1, 'day');

  useEffect(() => {
    initYuanGong();
    // 设置默认id
    if (isLocal()) setDefYuanGongId('10');
  }, []);

  // 同步记录列表
  const request = async (params: any) => {
    console.log('params: ', params);
    const res: any = await tongBuCWListApi(params);
    console.log('res:同步记录列表 ', res.data.list, res.data.list[0]);
    if (!isSuccess(res)) return { data: [], total: 0 };
    return { data: res?.data?.list, total: res?.data?.total, success: true };
  };

  const columns: any = [
    yuanGongLie(yuanGongs, defYuanGongId, true),
    {
      title: '用户(id)',
      dataIndex: 'userName',
      hideInSearch: true,
      render: (text: any, record: any) => (
        <p key="userid">
          {text}({record?.userId})
        </p>
      ),
    },
    {
      title: '方向',
      dataIndex: 'userName',
      hideInSearch: true,
      render: (text: any, record: any) => (
        <>
          <div className={`${record?.direction === -1 && 'color-red'}`}>
            {record?.direction === 1 ? '正向' : '反向'}
          </div>
        </>
      ),
    },
    // {
    //   title: '状态',
    //   dataIndex: 'is_active',
    //   valueType: 'select',
    //   valueEnum: { 1: '启用', 0: '停用' },
    //   initialValue: '1',
    //   render: (text: any, record: any) => (
    //     <div className={` ${record?.is_active ? 'color-green' : 'color-red'}`}>
    //       {record?.is_active ? '启用' : '停用'}
    //     </div>
    //   ),
    // },
    {
      title: '同步时间',
      dataIndex: 'tongBuTime',
      hideInSearch: true,
    },
    {
      title: '同步时间',
      dataIndex: 'dateTime',
      valueType: 'dateRange',
      hideInTable: true,
      search: { transform: ([startTime, endTime]: any) => ({ startTime, endTime }) },
      // initialValue: [defSearchTime, defSearchTime],
    },
    {
      title: '开始/结束时间',
      dataIndex: 'startTime',
      hideInSearch: true,
      render: (text: any, record: any) => (
        <>
          {text} <br />
          {record?.endTime}
        </>
      ),
    },
    {
      title: '数量',
      hideInSearch: true,
      dataIndex: 'pingCangNum',
    },
    {
      title: '之前盈亏',
      hideInSearch: true,
      dataIndex: 'yingKuiZhiQian',
    },
    {
      title: '本次盈亏',
      hideInSearch: true,
      dataIndex: 'yingKuiBenCi',
    },
    {
      title: '总盈亏',
      hideInSearch: true,
      dataIndex: 'yingKuiAll',
    },
  ];

  // 员工列表
  const initYuanGong = async () => {
    const yuanGongs: any = await getYuanGong(userListApi);
    console.log('yuanGongs:员工列表1112 ', yuanGongs);
    setYuanGongs(yuanGongs);
  };

  return (
    <>
      {yuanGongs && Object.keys(yuanGongs).length > 0 && (
        <ProTable
          headerTitle=" "
          rowKey="id"
          columns={columns}
          request={request}
          actionRef={tableRef}
          search={{ defaultCollapsed: false }}
          options={false}
          scroll={{ x: 1000 }}
        />
      )}
    </>
  );
}
